import { Component, OnInit, Input, Injector, Output } from '@angular/core';
import { DrillFormControl, HttpService } from '../../../..';
import { FormViewBaseComponent } from '../form-view-base.component';
import { DrillFormService } from '../../../../core/service/drill-form.service';

@Component({
  selector: 'app-drill-preview',
  templateUrl: './drill-preview.component.html',
  styleUrls: ['./drill-preview.component.scss']
})
export class DrillPreviewComponent extends FormViewBaseComponent implements OnInit {
  _recordId = 0;
  @Input()
  set recordId(input: any) {
    this._recordId = input;
  }
  recordFlowId = 0;
  // 审核数据的流程下标
  _reViewIndex = 0;
  @Input()
  set reViewIndex(input: any) {
    this._reViewIndex = input;
    this.iniPreviewContent();
  }
  flowRecordOutput: any;
  currentIndex = 0;

  /**
   * 获取文件名称
   * @param value 存储值
   */
  getFileName(value: string): string {
    const findOne = this.fileList.filter(e => e['FormName'] === value);
    if (findOne.length > 0) {
      return findOne[0].FileName;
    }
  }

  /**
   * 获取文件地址
   * @param value 存储值
   */
  getFileUrl(value: string): string {
    const findOne = this.fileList.filter(e => e['FormName'] === value);
    if (findOne.length > 0) {
      return this.httpService.IP_PORT + findOne[0].FileData;
    }
  }


  private async iniPreviewContent() {
    const findLog = await this.httpService
      .get<any>(`DrillRecord/GetDrillRecordLog?recordId=${this._recordId}&flowIndex=${this._reViewIndex}`);
    if (findLog) {
      this.focusEntity = JSON.parse(findLog.dataJson);
      this.fileList = JSON.parse(findLog.recordUploadFiles);
      this.arSystemFormControls = JSON.parse(findLog.formJson);
      this.arTableFormControls = this.drillFormService.getTableFormControls(this.arSystemFormControls);
    } else {
      this.focusEntity = {};
      this.fileList = [];
      this.arSystemFormControls = null;
      this.arTableFormControls = null;
    }
  }

  constructor(injector: Injector,
    public httpService: HttpService) {
    super(injector);
  }

  ngOnInit() {

  }
}
